<?php
	require_once PATH_OBJECT.'dataObject.php';
	
class dataObject_prestation extends dataObject
{

    /* @desc    Identifiant de la prestation
     * @var     int
     */
    public $_IDENTIFIANT_PRESTATION  = 0;
    
    
	/* @desc    montant de la prestation
     * @var     float
     */
    public $_MONTANT_PRESTATION  = 0;
    
	
	/* @desc    type de la prestation
     * @var     enum('U', 'A')
     */
    public $_TYPE_PRESTATION  = '';
    
	
	/* @desc    état de la prestation
     * @var     enum('A', 'R')
     */
    public $_ETAT_PRESTATION  = '';
    
    const ETAT_A_REGLER = 'A';
    const ETAT_REGLE = 'R';
   
	
	/* @desc    Identifiant sinistre concerné par la prestation
     * @var     int
     */
    public $_IDENTIFIANT_SINISTRE  = 0;

    
    
  public function __construct( $iId=0 )
    {
       parent::__construct();

       if ( $iId != 0 )
       {
            $sQuery = "SELECT   *   /*+ dataObject_prestation::__construct */  
                       FROM prestation 
            		   WHERE Identifiant_prestation = ".$iId;
            
            $aRows =  self::$_oConnexion->query($sQuery);
	  		$aRow = $aRows[0];
            $this->_IDENTIFIANT_PRESTATION  = $iId;
			$this->_MONTANT_PRESTATION 		= $aRow["Montant_prestation"];
			$this->_TYPE_PRESTATION 		= $aRow["Type_prestation"];
			$this->_ETAT_PRESTATION 		= $aRow["Etat_prestation"];
			$this->_IDENTIFIANT_SINISTRE 	= $aRow["Identifiant_sinistre"];
       }
    }
    /**
     * récupere la table prestation selon le(s) criter(s) précisés ou pas 
	 */
    function prestation_select( $sCritere = null )
    {
        $aRow = false;
    	$sQuery = "SELECT   *    
                   FROM prestation 
            	   WHERE 1=1 
            	   {$sCritere}";
        $aRow =  self::$_oConnexion->query($sQuery);

        return $aRow;
    }
   
    
    //liste prestation sinistre
  function prestation_getListePrestationSinistre($idSinistre)
    {

    	$sQuery = "SELECT distinct pr.Identifiant_prestation, Type_prestation, Montant_prestation, Etat_prestation, 
    					Motif_refus_sinistre, Date_evenement
                   FROM prestation pr
                   INNER JOIN  sinistres sin on sin.Identifiant_sinistre = pr.Identifiant_sinistre
                   LEFT JOIN evenement ev on pr.Identifiant_prestation = ev.Identifiant_prestation
            	   WHERE sin.Identifiant_sinistre='".$idSinistre."' "  ;
    	
        $aRow =  self::$_oConnexion->query($sQuery);

        return $aRow;
    }
    
    
	
	public function prestation_insert()
    {
    	$bId = false;
    	$sQuery = "INSERT INTO prestation (
				Montant_prestation ,
				Type_prestation ,
				Etat_prestation ,
				Identifiant_sinistre
    			) values (
			$this->_MONTANT_PRESTATION ,
			'$this->_TYPE_PRESTATION' ,
			'$this->_ETAT_PRESTATION' ,
			$this->_IDENTIFIANT_SINISTRE
    			)";
    	
        	$bId = self::$_oConnexion->execute($sQuery);
    	return $bId;
    }
    
    public function prestation_update()
    {
    	$sQuery = "UPDATE prestation SET 
	    		 Montant_prestation	= $this->_MONTANT_PRESTATION 
				 Type_prestation = $this->_TYPE_PRESTATION 
				 Etat_prestation  = $this->_ETAT_PRESTATION 
				 Identifiant_sinistre = $this->_IDENTIFIANT_SINISTRE
	    		WHERE Identifiant_prestation 	= ".$this->_IDENTIFIANT_PRESTATION ;
    	
    	return self::$_oConnexion->execute($sQuery);
    }
	
	public function prestation_delete($IdPrestation)
    {
    	$sQuery = "DELETE prestation WHERE Identifiant_prestation 	= ".$IdPrestation;
    	
    	return self::$_oConnexion->execute($sQuery);
    }
    
    // Vérifie si le montant du règlement est égal à celui de la prestation
    public function prestation_verifMontant($sMtt)
    {
        $aRow = false;
    	$sQuery = "SELECT  *    
                   FROM prestation 
            	   WHERE Identifiant_prestation = ".$this->_IDENTIFIANT_PRESTATION." AND Montant_prestation = '".$sMtt."'";
        $aRow =  self::$_oConnexion->query($sQuery);
		
        if(!empty($aRow)) 
        	return true;
        else return false;
    }
    
    
public function getMontantPrestation ($iIdsinistre)
	{
		$aRow = false;
		$sCritere = "SELECT DISTINCT Montant_prestation
    				FROM sinistres s, prestation pre 
    				WHERE  pre.Identifiant_sinistre = s.Identifiant_sinistre ";
		       
		
		
		$aRow =  self::$_oConnexion->query($sCritere);
		return $aRow[0]['Montant_prestation'];
	}

}
?>